#!/bin/sh

e2e_test_install() {
  NODE_LABEL_KEY="$(random_string)"
  NODE_LABEL_VALUE="$(random_string)"

  kubectl create namespace "$CLUSTER_NAMESPACE"

  cat <<EOF | kubectl apply -f -
apiVersion: stackgres.io/v1
kind: SGInstanceProfile
metadata:
  name: size-s
  namespace: $CLUSTER_NAMESPACE
spec:
  cpu: '500m'
  memory: '512Mi'
EOF

  DISTRIBUTEDLOGS_NAME="$(get_sgdistributedlogs_name "$CLUSTER_NAME-logs")"
}


e2e_test() {
  run_test "A stackgres cluster with ignore label should not be created" ignore_sgcluster_create

  run_test "A stackgres cluster with ignore label should not be patched" ignore_sgcluster_patch

  run_test "A stackgres cluster without ignore label should be patched" resume_sgcluster_reconciliation

  run_test "A distributed logs cluster with ignore label should not be created" ignore_sgdistributedlogs_create

  run_test "A distributed logs cluster with ignore label should not be patched" ignore_sgdistributedlogs_patch
  
  run_test "A distributed logs cluster without ignore label should be patched" resume_sgdistributedlogs_reconciliation
}

ignore_sgcluster_create(){
    cat << EOF | kubectl apply -f -
apiVersion: stackgres.io/v1
kind: SGCluster
metadata:
  name: $CLUSTER_NAME
  namespace: $CLUSTER_NAMESPACE
  annotations:
    stackgres.io/reconciliation-pause: "true"
spec:
  instances: 1
  postgres:
    version: "$E2E_POSTGRES_VERSION"
  pods:
    persistentVolume:
      size: '128Mi'
    scheduling:
      nodeSelector:
        $NODE_LABEL_KEY: $NODE_LABEL_VALUE"
  sgInstanceProfile: 'size-s'
  nonProductionOptions:
    disableClusterPodAntiAffinity: true
EOF

  if wait_until -t "15" has_cluster_generated_resources
  then
    fail "Cluster where created"
  else
    success "Cluster resources wheren't created"
  fi

}

ignore_sgcluster_patch(){
  kubectl annotate sgcluster "$CLUSTER_NAME" -n "$CLUSTER_NAMESPACE" "stackgres.io/reconciliation-pause-"

  wait_until has_cluster_generated_resources

  kubectl annotate sgcluster "$CLUSTER_NAME" -n "$CLUSTER_NAMESPACE" "stackgres.io/reconciliation-pause=true"

  kubectl patch sgcluster -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" --type='json' \
    -p='[{"op": "replace", "path": "/spec/instances", "value": 2}]'

  if wait_until -t "15" eval '[ $(get_cluster_replicas) = 2 ]'
  then
    fail "SGCluster is not being ignored"
  else
    success "SGCluster is being ignored"
  fi
  
}

resume_sgcluster_reconciliation(){
  kubectl annotate sgcluster "$CLUSTER_NAME" -n "$CLUSTER_NAMESPACE" "stackgres.io/reconciliation-pause"-

  if wait_until -t "15" eval '[ $(get_cluster_replicas) = 2 ]'
  then
    success "SGCluster is not being ignored"    
  else
    fail "SGCluster is being ignored"
  fi
}

ignore_sgdistributedlogs_create(){
  cat <<EOF | kubectl apply -f -
apiVersion: stackgres.io/v1
kind: SGDistributedLogs
metadata:
  name: $DISTRIBUTEDLOGS_NAME
  namespace: $CLUSTER_NAMESPACE
  annotations:
    stackgres.io/reconciliation-pause: "true"
spec:
  persistentVolume:
    size: '128Mi'
  nonProductionOptions:
    disableClusterPodAntiAffinity: true
EOF

  if wait_until -t "15" has_logs_generated_resources
  then
    fail "SGDistributedLogs cluster where created"
  else
    success "SGDistributedLogs cluster resources wheren't created"
  fi
}

ignore_sgdistributedlogs_patch(){
  kubectl annotate sgdistributedlogs "$DISTRIBUTEDLOGS_NAME" -n "$CLUSTER_NAMESPACE" "stackgres.io/reconciliation-pause=false" --overwrite

  wait_until has_logs_generated_resources

  kubectl annotate sgdistributedlogs "$DISTRIBUTEDLOGS_NAME" -n "$CLUSTER_NAMESPACE" "stackgres.io/reconciliation-pause=true"  --overwrite

  kubectl patch sgdistributedlogs -n "$CLUSTER_NAMESPACE" "$DISTRIBUTEDLOGS_NAME" --type='json' \
    -p='[{"op": "replace", "path": "/spec/metadata", "value": {"annotations":{"clusterPods":{"test":"test"}}}}]'

  if wait_until -t "15" eval '[ "$(get_sgdistributedlogs_annotations)" = '' ]'
  then
    fail "SGDistributedLogs is not being ignored"
  else
    success "SGDistributedLogs is being ignored"
  fi
}

resume_sgdistributedlogs_reconciliation(){
  kubectl annotate sgdistributedlogs "$DISTRIBUTEDLOGS_NAME" -n "$CLUSTER_NAMESPACE" "stackgres.io/reconciliation-pause=false" --overwrite

  if wait_until -t "15" eval '[ "$(get_sgdistributedlogs_annotations)" = test ]'
  then
    success "SGDistributedLogs is not being ignored"    
  else
    fail "SGDistributedLogs is being ignored"
  fi
}

has_logs_generated_resources(){
  kubectl get sgcluster -n "$CLUSTER_NAMESPACE" "$DISTRIBUTEDLOGS_NAME"
}

get_cluster_replicas() {
  kubectl get sts -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" -o jsonpath='{.spec.replicas}'
}

get_sgdistributedlogs_annotations() {
  kubectl get sgcluster -n "$CLUSTER_NAMESPACE" "$DISTRIBUTEDLOGS_NAME" -o jsonpath='{.spec.metadata.annotations.clusterPods.test}'
}
